package ch.areion.areionapp;

import ch.areion.areionapp.core.Controller;
import ch.areion.areionapp.core.Settings;
import ch.areion.areionapp.core.Testing;
import ch.areion.areionapp.communication.ActHTTP;
import ch.areion.areionapp.communication.Monitor;
import ch.areion.areionapp.engine.Engine;
import ch.areion.areionapp.engine.IEngine;
import ch.areion.areionapp.engine.MotorDriver.Control;
import ch.areion.areionapp.R;
import ch.areion.common.message.Module;
import ch.areion.common.message.Type;
import ch.areion.common.message.Version;

import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;

public class Main extends IOIOActivity {
	private static Controller contr;
	private Button button_driveForward;
	private Button button_driveParcour;
	private Button button_stopAreion;
	private static Main instance;

	/**
	 * this method will called every time when the application is started.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
		    "Starting application");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Main.instance = this;
		button_driveForward = (Button) findViewById(R.id.button_driveForward);
		button_driveParcour = (Button) findViewById(R.id.button_driveParcour);
		button_stopAreion = (Button) findViewById(R.id.button_stopAreion);

		button_stopAreion.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try {
					Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
					    "Test, stopAreion");
					IEngine eng = Controller.getEngine();
					eng.stop();
				}
				catch(ConnectionLostException e) {
					Monitor.getInstance().add(Version.V01, Type.ERROR, Module.CORE,
					    "Connection lost to IOIO. " + e.getMessage());
					e.printStackTrace();

				}
				catch(NullPointerException e) {
					Monitor.getInstance().add(Version.V01, Type.ERROR, Module.CORE,
					    "engine is null. Message: " + e);
				}
				catch(InterruptedException e) {
					Monitor.getInstance().add(Version.V01, Type.ERROR, Module.CORE,
					    "Testing interrupted. " + e.getMessage());
					e.printStackTrace();
				}
			}
		});
		;

		button_driveForward.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try {
					Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
					    "Test, driveForward");
					IEngine eng = Controller.getEngine();
					eng.driveStraight(4);
				}
				catch(ConnectionLostException e) {
					Monitor.getInstance().add(Version.V01, Type.ERROR, Module.CORE,
					    "Connection lost to IOIO. " + e.getMessage());
					e.printStackTrace();
				}
				catch(NullPointerException e) {
					Monitor.getInstance().add(Version.V01, Type.ERROR, Module.CORE,
					    "engine is null. Message: " + e);

				}
				catch(InterruptedException e) {
					Monitor.getInstance().add(Version.V01, Type.ERROR, Module.CORE,
					    "Testing interrupted. " + e.getMessage());
					e.printStackTrace();
				}
			}
		});

		button_driveParcour.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try {
					Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
					    "Test, driveParcour");
					// contr.testWallFollower();
//					contr.test();
					contr.start();
//					 contr.startAfterQRCODE();
				}
				catch(ConnectionLostException e) {
					Monitor.getInstance().add(Version.V01, Type.ERROR, Module.CORE,
					    "Connection lost to IOIO. " + e.getMessage());
					e.printStackTrace();
				}
				catch(InterruptedException e) {
					Monitor.getInstance().add(Version.V01, Type.ERROR, Module.CORE,
					    "Testing interrupted. " + e.getMessage());
					e.printStackTrace();
				}
			}
		});

		/*
		 * Can be removed later, after the menu option worked -ir-
		 * 
		 * button_http = (Button) findViewById(R.id.button_http);
		 * button_http.setOnClickListener(new OnClickListener() { public void
		 * onClick(View v) {
		 * 
		 * Intent intent = new Intent(Main.this, ActHTTP.class);
		 * startActivity(intent); } });
		 * 
		 * button_scan = (Button) findViewById(R.id.button_scan);
		 * button_scan.setOnClickListener(new OnClickListener() {
		 * 
		 * public void onClick(View v) { IntentIntegrator integrator = new
		 * IntentIntegrator(Main.this); integrator.initiateScan(); }
		 * 
		 * });
		 */
		Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
		    "Application started");
	}

	/**
	 * this method disable the back button.
	 */
	@Override
	public void onBackPressed() {}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * QR-Code handling
	 */
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode,
		    resultCode, intent);
		if(scanResult != null) {
			// handle scan result
			String result = scanResult.getContents();
			// create intent
			Intent intent_next = new Intent(Main.this, ActHTTP.class);
			// Intent mit den Daten fuellen
			intent_next.putExtra("scan", result);
			startActivity(intent_next);
		}
		// else continue with any other code you need in the method

	}

	/**
	 * menu selection
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case R.id.menu_quit:
				Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
				    "Application closing");
				contr.disconnect();
				System.exit(0);
				return true;

			case R.id.menu_settings:
				Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
				    "Switch to Settings Layout");
				Intent intentSettings = new Intent(Main.this, Settings.class);
				startActivity(intentSettings);
				return true;

			case R.id.menu_testing:
				Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
				    "Switch to Testing Layout");
				Intent intentTesting = new Intent(Main.this, Testing.class);
				startActivity(intentTesting);
				return true;

			case R.id.menu_http:
				Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
				    "Switch to HTTP Layout");
				Intent intent = new Intent(Main.this, ActHTTP.class);
				startActivity(intent);
			case R.id.menu_scanQR:
				Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
				    "Switch to QR-Scanner Layout");
				IntentIntegrator integratorScanQR = new IntentIntegrator(Main.this);
				integratorScanQR.addExtra("SCAN_WIDTH", 700);
				integratorScanQR.addExtra("SCAN_HEIGHT", 400);
				integratorScanQR.initiateScan();
			default:
				return super.onOptionsItemSelected(item);
		}

	}

	/**
	 * Creates the IOIO thread Controller, which establishes and maintains a
	 * connection to IOIO.
	 */
	@Override
	protected IOIOLooper createIOIOLooper() {
		Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
		    "createIOIOLooper starting....");
		contr = new Controller();

		Monitor.getInstance().add(Version.V01, Type.INFO, Module.CORE,
		    "IOIOLooper created....");
		return contr;
	}

	/**
	 * this method return the controller instance.
	 * 
	 * @return controller instance
	 */
	public static Controller getControllerInstance() {
		return contr;
	}

	public static Main getMain() {
		return Main.instance;
	}
}
